package com.itcam.thread_;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Author:Cammy.Wu
 * Date:2024-05-14
 * Description:等待唤醒机制
 * <p>
 * void await();   指定等待线程
 * void signal();    指定唤醒单个等待线程
 */

public class CorrespondenceDemo03 {
    public static void main(String[] args) {
        Printer3 p3 = new Printer3();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        p3.print1();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        p3.print2();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        p3.print3();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

    }
}

class Printer3 {

    ReentrantLock lock = new ReentrantLock();

    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();

    int flag = 1;

    public void print1() throws InterruptedException {

        lock.lock();

        if (flag != 1) {
            c1.await();
        }
        System.out.print("指");
        System.out.print("环");
        System.out.print("上");
        System.out.print("铭");
        System.out.print("记");
        System.out.print("着");
        System.out.print("我");
        System.out.print("们");
        System.out.print("的");
        System.out.print("光");
        System.out.print("阴");
        System.out.println();

        flag = 2;

        c2.signal();
        lock.unlock();

    }

    public void print2() throws InterruptedException {
        lock.lock();

        if (flag != 2) {
            c2.await();
        }
        System.out.print("雅");
        System.out.print("俗");
        System.out.print("共");
        System.out.print("赏");
        System.out.println();

        flag =3;

        c3.signal();
        lock.unlock();
    }

    public void print3() throws InterruptedException {
        lock.lock();

        if (flag != 3) {
            c3.await();
        }
        System.out.print("画");
        System.out.print("江");
        System.out.print("湖");
        System.out.print("之");
        System.out.print("不");
        System.out.print("良");
        System.out.print("人");
        System.out.println();

        flag = 1;

        c1.signal();
        lock.unlock();
    }
}